package com.gjs.user.microservice.Impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.gjs.common.microservice.api.IUserMicroService;
import com.gjs.common.pojo.dto.user.UserInfoDTO;
import com.gjs.common.pojo.dto.user.UserQueryDTO;
import com.gjs.common.pojo.dto.user.UserRegisterDTO;
import com.gjs.user.pojo.entity.User;
import com.gjs.user.service.IUserService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;

import java.util.List;
import java.util.stream.Collectors;

@DubboService
@RequiredArgsConstructor
@Slf4j
public class UserMicroServiceImpl implements IUserMicroService {

    private final IUserService userService;

    @Override
    public UserDetails loadUserByUsername(String userAccountOrMobile) throws UsernameNotFoundException {
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper
                .eq(User::getUserAccount,userAccountOrMobile)
                .or()
                .eq(User::getMobile,userAccountOrMobile);
        User user = userService.getOne(wrapper);
        if(user == null){
            throw new UsernameNotFoundException("用户不存在");
        }
        return new org.springframework.security.core.userdetails.User(user.getUserAccount(), user.getPassword(), true, true, true, true, AuthorityUtils.NO_AUTHORITIES);
    }

    @Override
    public UserInfoDTO userRegister(UserRegisterDTO userRegisterDto) {
        return userService.userRegister(userRegisterDto);
    }

    @Override
    public boolean resetPassword(String mobile, String password) {
        return userService.resetPassword(mobile,password);
    }

    @Override
    public boolean checkUserExistByMobile(String mobile){
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getMobile,mobile);
        return userService.getOne(wrapper) != null;
    }

    @Override
    public List<UserInfoDTO> list(UserQueryDTO queryDTO){
        return userService.list(this.buildWrapper(queryDTO))
                .stream()
                .map(v-> BeanUtil.copyProperties(v, UserInfoDTO.class))
                .collect(Collectors.toList());
    }

    @Override
    public List<String> listUserSn(UserQueryDTO queryDTO){
        LambdaQueryWrapper<User> userWrapper = (LambdaQueryWrapper) this.buildWrapper(queryDTO);
        return userService.getBaseMapper().selectObjs(userWrapper.select(User::getUserSn))
                .stream()
                .map(Object::toString)
                .collect(Collectors.toList());
    }

    @Override
    public UserInfoDTO getOne(UserQueryDTO queryDTO) {
        Wrapper<User> userWrapper = this.buildWrapper(queryDTO);
        if(userWrapper.isEmptyOfWhere()){
            return null;
        }
        User user = userService.getOne(userWrapper);
        return user == null ? null : BeanUtil.copyProperties(user,UserInfoDTO.class);
    }

    private Wrapper<User> buildWrapper(UserQueryDTO queryDTO) {
        return userService.buildWrapper(queryDTO);
    }
}
